﻿Module mdl_Common
    Public Function getMsgStr(ByVal msgId As String, ByVal ParamArray values() As String) As String
        Dim format = htMsgMst(msgId)
        For Each i In values.Select(Function(Value, Index) New With {Value, Index})
            format = format.Replace("{" + i.Index.ToString() + "}", i.Value.ToString())
        Next
        Return format
    End Function

    Public Sub setAllErrMsg(ByVal lang As String)
        If htMsgMst.Count = 0 Then
            Dim dtMsgMst As DataTable = g_DBSFCommon.GetAllMsg(lang)
            For Each dr In dtMsgMst.Rows
                With htMsgMst
                    .Add(dr("MESSAGE_ID"), dr("MESSAGE"))
                End With
            Next
        End If
    End Sub

    Public Function setEndTime() As DateTime
        Dim dbCondData As New clsDbCondData
        Dim DBTime As DateTime
        Dim str9Time As String
        Dim strNext9Time As String
        Dim str21Time As String

        DBTime = dbCondData.GetServerTime()
        str9Time = String.Format("{0}/{1}/{2} 09:20:00", DBTime.Year, DBTime.Month, DBTime.Day)

        str21Time = String.Format("{0}/{1}/{2} 21:20:00", DBTime.Year, DBTime.Month, DBTime.Day)

        If DBTime.Hour < 9 Then '0～9時
            DBTime = CDate(str9Time)
        ElseIf DBTime.Hour >= 9 And DBTime.Hour < 21 Then '9～21時
            DBTime = CDate(str21Time)
        ElseIf DBTime.Hour >= 21 Then '21時後
            DBTime = DBTime.AddDays(1)
            strNext9Time = String.Format("{0}/{1}/{2} 09:20:00", DBTime.Year, DBTime.Month, DBTime.Day)
            DBTime = CDate(strNext9Time)
        End If

        Return DBTime

    End Function

    Public Function setEndTime2() As DateTime
        Dim dbCondData As New clsDbCondData
        Dim DBTime As DateTime
        Dim str9Time As String
        Dim strNext9Time As String
        Dim str21Time As String

        DBTime = dbCondData.GetServerTime()
        str9Time = String.Format("{0}/{1}/{2} 09:00:00", DBTime.Year, DBTime.Month, DBTime.Day)

        str21Time = String.Format("{0}/{1}/{2} 21:00:00", DBTime.Year, DBTime.Month, DBTime.Day)

        If DBTime.Hour < 9 Then '0～9時
            DBTime = CDate(str9Time)
        ElseIf DBTime.Hour >= 9 And DBTime.Hour < 21 Then '9～21時
            DBTime = CDate(str21Time)
        ElseIf DBTime.Hour >= 21 Then '21時後
            DBTime = DBTime.AddDays(1)
            strNext9Time = String.Format("{0}/{1}/{2} 09:00:00", DBTime.Year, DBTime.Month, DBTime.Day)
            DBTime = CDate(strNext9Time)
        End If

        Return DBTime

    End Function

    Public Sub outputExcel(dgv As DataGridView)
        Dim saveFileName As String = ""
        Dim saveDialog As New SaveFileDialog()

        If IsNothing(dgv.DataSource) Then
            'MsgBox(getMsgStr("MSG10048"))
            Exit Sub
        End If

        saveDialog.DefaultExt = "xls"
        saveDialog.Filter = "Excel文件|*.xlsx"
        'saveDialog.FileName = "a"
        saveDialog.ShowDialog()
        saveFileName = saveDialog.FileName
        If (saveFileName.IndexOf(":") < 0) Then Return ' 被点了取消
        Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
        If xlApp Is Nothing Then
            getMsgStr("MSG10003", "Excel")
            Return
        End If

        Dim workbooks As Microsoft.Office.Interop.Excel.Workbooks = xlApp.Workbooks
        Dim workbook As Microsoft.Office.Interop.Excel.Workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet)
        Dim worksheet As Microsoft.Office.Interop.Excel.Worksheet = workbook.Worksheets(1) '取得sheet1
        '写入标题
        'For (i as integer = 0, i < dgvList.ColumnCount i++)
        For i As Integer = 0 To dgv.ColumnCount - 1
            worksheet.Cells(1, i + 1) = dgv.Columns(i).HeaderText
        Next

        '写入数值
        For r As Integer = 0 To dgv.Rows.Count - 1
            For i As Integer = 0 To dgv.ColumnCount - 1
                worksheet.Cells(r + 2, i + 1) = dgv.Rows(r).Cells(i).Value
            Next
            System.Windows.Forms.Application.DoEvents()
        Next
        worksheet.Columns.EntireColumn.AutoFit() '列宽自适应
        If saveFileName <> "" Then

            Try

                workbook.Saved = True
                workbook.SaveCopyAs(saveFileName)
            Catch ex As Exception
                MsgBox(getMsgStr("MSG00010", {"Excel生成", "文件可能正被打开"}))
            End Try

        End If

        xlApp.Quit()
        GC.Collect() '强行销毁
        MsgBox(getMsgStr("MSG00019", saveDialog.FileName))
    End Sub

End Module
